package org.ldh.dao.impl;

import org.ldh.dao.BookTypeDao;
import org.ldh.domain.BookType;
import org.ldh.util.ConnectionFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 图书类别的持久层实现类
 */
public class BookTypeDaoImpl implements BookTypeDao {

    @Override
    public List<BookType> findAll() {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from bookType";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //执行sql
            rs = pstm.executeQuery();
            List<BookType> bookTypes = new ArrayList<BookType>();
            //指针默认指向表头，需向下移动一行
            while(rs.next()){
                //封装实体类
                BookType bookType = new BookType();
                bookType.setBookTypeId(rs.getString("bookType_id"));
                bookType.setBookTypeName(rs.getString("bookType_name"));

                bookTypes.add(bookType);
            }
            return bookTypes;
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public BookType findById(String bookTypeId) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from bookType where bookType_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setInt(1,Integer.parseInt(bookTypeId));
            //执行sql
            rs = pstm.executeQuery();
            //指针默认指向表头，需向下移动一行
            while(rs.next()){
                //封装实体类
                BookType bookType = new BookType();
                bookType.setBookTypeId(rs.getString("bookType_id"));
                bookType.setBookTypeName(rs.getString("bookType_name"));

                return bookType;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public void save(BookType bookType) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try{
            //编写sql
            String sql = "insert into bookType(bookType_id,bookType_name) values(?,?)";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1,bookType.getBookTypeId());
            pstm.setString(2,bookType.getBookTypeName());
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }

    @Override
    public void update(BookType bookType) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try{
            //编写sql
            String sql = "update bookType set bookType_name = ? where bookType_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1,bookType.getBookTypeName());
            pstm.setString(2,bookType.getBookTypeId());
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }

    @Override
    public void delete(String bookTypeId) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try{
            //编写sql
            String sql = "delete from bookType where bookType_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1,bookTypeId);
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }
}
